package com.atguigu.crowd.mvc.controller;

import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.constant.CrowdConstan;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

/**
 * @author zzqstart
 * @Description
 * @date 2022-08-30 16:30
 */
@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("admin/update.html")
    public String update(Admin admin, @RequestParam("keyword")String keyword, @RequestParam("pageNum") Integer pageNum){
        adminService.updateAdmin(admin);
        return "redirect:/admin/page.html?keyword="+keyword+"&pageNum="+pageNum;
    }

    @RequestMapping("admin/to/edit.html")
    public String getAdminById(
            @RequestParam("adminId") Integer adminId,
            Model model
    ){

        Admin admin = adminService.getAdminById(adminId);

        model.addAttribute(CrowdConstan.ATTR_NAME_UPDATE_ADMIN,admin);

        return "admin-edit";
    }

    @PreAuthorize("hasAnyAuthority('user:save')")
    @RequestMapping("/admin/save.html")
    public String save(Admin admin){
        adminService.saveAdmin(admin);

        return "redirect:/admin/page.html?pageNum=" + Integer.MAX_VALUE;

    }

    @RequestMapping("/admin/remove/{adminId}/{pageNum}/{keyword}.html")
    public String remove(
            @PathVariable("adminId") Integer adminId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("keyword") String keyword,
            HttpSession session
    ) {

        Admin admin = (Admin) session.getAttribute(CrowdConstan.ATTR_NAME_LOGIN_ADMIN);

        adminService.remove(adminId, admin);

        return "redirect:/admin/page.html?pageNum=" + pageNum + "&keyword=" + keyword;

    }

    @RequestMapping("/admin/page.html")
    public String getAdminPage(
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            Model model
    ) {

        PageInfo<Admin> adminPage = adminService.getAdminPage(keyword, pageNum, pageSize);

        model.addAttribute(CrowdConstan.ATTR_NAME_PAGE_INFO, adminPage);

        return "admin-page";
    }

    @RequestMapping("/admin/do/login.html")
    public String adminLogin(
            @RequestParam("loginAcct") String loginAcct,
            @RequestParam("userPswd") String userPswd,
            HttpSession session
    ) {

        Admin admin = adminService.getAdminByLoginAcct(loginAcct);

        session.setAttribute(CrowdConstan.ATTR_NAME_LOGIN_ADMIN, admin);

        return "redirect:/admin/to/main/page.html";
    }

    @RequestMapping("/admin/do/logout.html")
    public String adminLogout(HttpSession session) {

        session.invalidate();

        return "redirect:/admin/to/login/page.html";
    }


}
